/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package INTERFAZ;

import CONTROLES.HabilitarLector;
import UTILITARIOS.Herramienta;
import java.awt.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;



/**
 *
 * @author Administrador
 */
public class PantallaPrincipal extends JFrame {

    private JPanel panelHuella;
    private TransparentPanel panelTitulo;
   
    private JLabel lblfonfo;

    private JPanel nuevoPanel;

    private JLabel lblTitulo;
    
    

    private BufferedImage imagenHuella=null;
    private HabilitarLector control = null;
   
    
    
    public PantallaPrincipal(){
        
        control = new HabilitarLector(this);
          
        inicializar();
       
        setProperties();
        buildComponents();
        setPropertiesComponents();
        addComponents();
        addEvents();
        

        
        
        

   }

    public void inicializar() {

       try {
           UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
       } catch (Exception e) {
           
       }
       
       //Crea una instancia de Util
       
   }

    

    private void setProperties() {
        setLayout(new BorderLayout());
		setSize(405, 500);
                
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                setTitle("Ventana de Identificacion");
		getContentPane().setBackground(Color.WHITE);
                setAlwaysOnTop(true);
              
		setResizable(false);
    }

    private void buildComponents() {
       
        panelHuella = new TransparentPanel();
        panelTitulo = new TransparentPanel();

       

        lblfonfo = new JLabel(new ImageIcon(getClass().getResource("/imagenes/fondo.png")));

       

        lblTitulo = new JLabel("SISTEMA DE IDENTIFICACION DEL CLIENTE");
        


    }

    private void setPropertiesComponents() {

                lblfonfo.setBounds(0, 0, 800, 600);

                //lblTitulo.setBounds(20,10,590, 50);
		lblTitulo.setFont( new Font("Tahoma", Font.BOLD, 15) );
                lblTitulo.setForeground(Color.WHITE);

                panelTitulo.setLayout(new FlowLayout());
		panelTitulo.setSize(new Dimension(400,60));
		panelTitulo.setLocation(0,0);
		panelTitulo.setBorder( Herramienta.createTitleBorder(".") );
                panelTitulo.setForeground(Color.WHITE);
                panelTitulo.setBackground(Color.BLACK);

                panelHuella.setLayout(new BorderLayout());
		panelHuella.setSize(new Dimension(300,375));
		panelHuella.setLocation(50, 60);
		panelHuella.setBorder(BorderFactory.createTitledBorder(null,"Huella Capturada",TitledBorder.CENTER , TitledBorder.DEFAULT_POSITION));
                panelHuella.setBackground(Color.WHITE);

                

                
    }

    private void addComponents() {

        panelTitulo.add(lblTitulo);
        

        add(panelTitulo);
        add(panelHuella);
        
       
         add(lblfonfo,BorderLayout.CENTER);
    }

    private void addEvents() {
        final PantallaPrincipal p = this;
        addWindowListener( new WindowAdapter(){
			@Override
			public void windowClosing(WindowEvent e) {
                            
                            
                            //  setVisible(false);
                           control.terminarCaptura();
                            dispose();
                            f.getBtnHuella().setEnabled(true);
                            f.getBarra().getBtnCliente().setEnabled(true);
                        }

                        public void windowActivated(WindowEvent e) {
                          
                             panelHuella.add(crearPanelHuella());

                          
                            }
		});

    
    

   

    }

    public JComponent crearPanelHuella() {
       //Crea un panel nuevo para mostrar la huella
       nuevoPanel = new JPanel(){

           //Se sobreescribe el método paintComponent para habilitar la muestra de la imagen de la huella
            @Override
           public void paint(Graphics g) {
               super.paintComponent(g);

               //Si hay una imagen para ser mostrada
              
               if (imagenHuella!=null) {
                  
                   //Calcula el tamaño y posición de la imagen para ser pintada
                   //el tamaño es ajustado para que ocupe todo el tamaño del panel
                   Insets insets = getInsets();
                   int transX = insets.left;
                   int transY = insets.top;
                   int width  = getWidth()  - getInsets().right  - getInsets().left;
                   int height = getHeight() - getInsets().bottom - getInsets().top;

                   //Se dibuja la imagen
                   g.drawImage(imagenHuella, transX, transY, width, height, null);
               }

           }

       };
       
       //Se agrega un border alrededor del panel
       nuevoPanel.setBorder(new CompoundBorder (
               new EmptyBorder (2,2,2,2),
               new BevelBorder(BevelBorder.LOWERED)));
        
       if(nuevoPanel==null)
       {
            System.exit(1);
            return null;
       }else{

       return nuevoPanel;
       }

   }

   

    public void showImage(BufferedImage image)
    {
       
        imagenHuella = image;
        repaint();
    }






    class TransparentPanel extends JPanel{

		private static final long serialVersionUID = 1L;
		private float tran= 0.6f;

		@Override
		protected void paintComponent(Graphics g) {
			Graphics2D g2 = (Graphics2D) g;
			g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
			AlphaComposite old = (AlphaComposite) g2.getComposite();
			g2.setComposite(AlphaComposite.SrcOver.derive(getTran()));
			super.paintComponent(g);
			g2.setComposite(old);
		}

		public float getTran() {
			return tran;
		}

		public void setTran(float tran) {
			this.tran = tran;
		}
	}

    public void cerrar()
    {
     /*   showImage(null);
        setVisible(false);
        f.setPantalla(this);
       */
        
       showImage(null);
       this.setExtendedState(JFrame.ICONIFIED);
        
    }

    public void abrir()
    {
        setVisible(true);
    }

    public static void main(String[] args) {
        PantallaPrincipal p = new PantallaPrincipal();
        p.setVisible(true);
       
    }
    FormPrincipal f;
    public void setParent(FormPrincipal f )
    {
        this.f = f;
    }
    

}
